package com.itheima.reggie.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.exception.BusinessException;
import com.itheima.reggie.mapper.EmployeeMapper;
import com.itheima.reggie.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author: Rodgers
 * @Date 2022/3/8 10:37
 */


@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired(required = false)
    private EmployeeMapper employeeMapper;

    /**
     * 登录的业务方法
     * @param employee
     * @return
     */
    @Override
    public R<Employee> login(Employee employee) {
        //1.校验用户是否存在
        //根据mapper用户名查找用户
        Employee loginEmployee = employeeMapper.login(employee);

        //如果用户名没有问题判断密码
        if (loginEmployee==null){
            throw new BusinessException("用户名或密码错误");
        }

        //2.校验密码是否正确
        //对用户输入的明文密码加密
        String md5 = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes(StandardCharsets.UTF_8));
        //再与数据库密文对比
        if (!loginEmployee.getPassword().equals(md5)){
            //如果不一样，提示用户去修改
            throw new BusinessException("用户名或密码错误");
        }

        //3.判断用户的状态是否为禁用
        if (loginEmployee.getStatus()==0){
            throw new BusinessException("您的账号已被禁用，请联系管理员解决");
        }


        //如果上面三者都正确则是登录成功
        return R.success(loginEmployee);
    }

    @Override
    public boolean save(Employee employee) {
        //校验账号username唯一
        Employee newBoy = employeeMapper.login(employee);
        //判断用户在数据库是否存在，存在则提示用户
        if (newBoy!=null){
            throw new BusinessException("账号已被注册!");
        }

        //完善信息
        //设置初始化密码
        employee.setPassword(DigestUtils.md5DigestAsHex("abc123456".getBytes()));
        employee.setStatus(1);
        //设置用户注册时间
        employee.setCreateTime(LocalDateTime.now());
        //设置用户修改时间
        employee.setUpdateTime(LocalDateTime.now());

        employeeMapper.save(employee);
        return true;
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public Page<Employee> findByPage(Integer page, Integer pageSize, String name) {
        //通过页面启动器，设置第几页和页面大小
        PageHelper.startPage(page, pageSize);

        //调用mapper层方法，模糊查询用户名集合
        List<Employee> names = employeeMapper.findByName(name);

        //使用PageInfo封装上面的集合
        PageInfo<Employee> pageInfo = new PageInfo<>(names);

        Page<Employee> pageObj = new Page<>();
        pageObj.setRecords(pageInfo.getList());
        pageObj.setTotal(pageInfo.getTotal());

        return pageObj;
    }

    /**
     * 修改
     * @param employee
     * @return
     */
    @Override
    public boolean update(Employee employee) {
        employee.setUpdateTime(LocalDateTime.now());
        return employeeMapper.update(employee)>0;
    }

    /**
     * 通过id查找
     * @param id
     * @return
     */
    @Override
    public R<Employee> selectById(Long id) {
        Employee employee = employeeMapper.selectById(id);
        return R.success(employee);
    }

}
